import {combination} from "../../utils/util";

class SkuCode {
    code
    spuId
    segments = []

    constructor(code) {
        this.code = code
        this._splitToSegments()
    }

    _splitToSegments() {
        const spuAndSpec = this.code.split('$')
        this.spuId = spuAndSpec[0]

        const specCodeArray = spuAndSpec[1].split('#')
        const length = specCodeArray.length

        for (let i = 0; i < length; i++) {
            // segments是二维数组 [["1-45", "3-9"],["1-45", "4-14"],["3-9", "4-14"]]
            const segments = combination(specCodeArray, i + 1)
            // 拼接后["1-45#3-9", "1-45#4-14", "3-9#4-14"]
            const newSegments = segments.map(segs => segs.join('#'))
            this.segments.push(...newSegments)
        }
    }
}

export {
    SkuCode
}
